package lambda.lambda_ideas.lambda_thought;

import lambda.lambda_ideas.base.Apple;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;

/**
 * Description: 带有行为参数化思想的苹果筛选器
 *
 * @author code-worker
 * @date 2021/10/03 10:38
 */
public class AppleFiltersWithLambdaThought {

    /**
     * task: 苹果筛选器
     * @author code-worker
     * @date 2021/10/3 10:48
     * @param inventory 苹果集合
     * @param predicate 筛选条件
     * @return java.util.List<lambda.lambda_ideas.base.Apple>
     */
    public static List<Apple> filterApples(List<Apple> inventory, Predicate<Apple> predicate){
        List<Apple> result = new ArrayList<>();
        for (Apple apple : inventory) {
            if(predicate.test(apple)){
                result.add(apple);
            }
        }
        return result;
    }

    public static void main(String[] args) {
        List<Apple> apples = Apple.initData();
        System.out.println("green apple: "+ filterApples(apples, Apple::isGreenApple));
        System.out.println("heavy apple: "+ filterApples(apples, Apple::isHeavyApple));
    }
}
